﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HT.Data.BussinessLogic;
using System.Data;
namespace Hotel.UserControls
{
  public partial class AddService : System.Web.UI.UserControl
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
        LoadData();
    }



    private void LoadData()
    {
      try
      {
        DataSet ds = HotelManger.GetInstance().GetAllRoomServices(0);
        if (ds.Tables[0].Rows.Count > 0)
        {
          gvServices.DataSource = ds;
          gvServices.DataBind();
        }
        else
        {
          DataTable dt = new DataTable();
          dt.Columns.Add("ServiceId");
          dt.Columns.Add("ServiceName");
          dt.Columns.Add("Edit");
          dt.Columns.Add("Delete");
          DataRow dr = dt.NewRow();
          dt.Rows.Add(dr);
          gvServices.DataSource = dt;
          gvServices.DataBind();
          gvServices.Rows[0].Visible = false;
        }
      }
      catch (Exception ex)
      {

      }
    }

    protected void gvServices_RowEditing(object sender, GridViewEditEventArgs e)
    {
      gvServices.EditIndex = e.NewEditIndex;
      LoadData();
    }

    protected void gvServices_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

      gvServices.EditIndex = -1;
      LoadData();
    }

    protected void gvServices_RowCommand(object sender, GridViewCommandEventArgs e)
    {
      if (e.CommandName.Equals("Insert"))
      {
        if (!string.IsNullOrEmpty(((TextBox)gvServices.FooterRow.FindControl("txtNameNew")).Text.Trim()))
        {
          HotelManger.GetInstance().SaveService(((TextBox)gvServices.FooterRow.FindControl("txtNameNew")).Text.Trim(),"raghu");
          LoadData();
        }
      }

    }

    protected void gvServices_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
      int id = Convert.ToInt32(gvServices.DataKeys[e.RowIndex].Values[0].ToString());
      HotelManger.GetInstance().DeleteService(id);
      LoadData();
    }

    protected void gvServices_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
      int id = Convert.ToInt32(gvServices.DataKeys[e.RowIndex].Values[0].ToString());
      string name = ((TextBox)gvServices.Rows[e.RowIndex].FindControl("txtName")).Text;
      HotelManger.GetInstance().UpdateService(id, name,"raghu");
      gvServices.EditIndex = -1;
      LoadData();
    }


  }
}